package com.chatSystem;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class Server {
    static ServerSocket server = null;
    static Socket socket = null;
    static List<Socket> list = new ArrayList<Socket>();//存客户端
    static Socket[] user = new Socket[5];
    public static int[] haveUser = new int[5];
    static boolean xuanOnLine = false;
    static boolean taoOnLine = false;
    static boolean yuanOnLine = false;


    public static Socket[] getSocket(){
        return user;
    }
    public static boolean isXuanOnLine(){
        if(haveUser[0] == 1) {
            xuanOnLine = true;
        }
        return xuanOnLine;
    }
    public static boolean isTaoOnLine(){
        if(haveUser[1] == 1) {
            taoOnLine = true;
        }
        return taoOnLine;
    }
    public static boolean isYuanOnLine(){
        if(haveUser[2] == 1) {
            yuanOnLine = true;
        }
        return yuanOnLine;
    }


    public static void main(String[] args) {

        ServerWindow serverWindow = new ServerWindow();//新建聊天服务器界面
        try {
            server = new ServerSocket(8081);//服务器端套接字

            //等待连接，连接后开启线程
            while(true){
                socket = server.accept();//等待连接
                list.add(socket);//添加当前客户端到列表
                int x = list.size();
                System.out.println(list.get(x-1));
                int i = 0;
                user[i] = list.get(x-1);
//                haveUser[i] = 1;
//                System.out.println(user[i].getPort());
//                i++;

                //在服务器为客户端开启线程
                ServerReadAndPrint readAndPrint = new ServerReadAndPrint(socket,serverWindow);
                readAndPrint.start();
            }
        }catch (IOException e){
            e.printStackTrace();
        }

    }
}




class ServerReadAndPrint extends Thread{
    Socket thisSocket = null;
    ServerWindow serverWindow = null;
    BufferedReader bufferedReader = null;
    BufferedWriter bufferedWriter = null;
    PrintWriter out = null;
    JButton Xuan = ServerWindow.getXuan();
    JButton Tao = ServerWindow.getTao();
    JButton Yuan = ServerWindow.getYuan();
    public static int[] haveUser = new int[5];
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public ServerReadAndPrint(Socket s, ServerWindow serverWindow) {
        super();
        this.serverWindow = serverWindow;  // 获取多人聊天系统界面
        this.thisSocket = s;  // 获取当前客户端信息
    }
    public ServerReadAndPrint(){}

    class serverWindowListen implements ActionListener{
        BufferedWriter bw;

        public void actionPerformed(ActionEvent event) {
            JButton jbuttonTemp = (JButton) event.getSource();
            if(jbuttonTemp == Xuan){
                String s1 = "服务器消息:您已被下线，消息将不会再被递送到其他客户端@xuan";
                System.out.println("获取动作成功！");
                haveUser[0] = 0;
                try {
                    for(Socket socket: Server.list) {
                        bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                        out = new PrintWriter(socket.getOutputStream());  // 对每个客户端新建相应的socket套接字
                        //if (socket == user[0]) {  // 发送给当前客户端
                            //bw.write(s1);
                            out.println(simpleDateFormat.format(new Date()) + s1);
                            out.flush();
                        //}
                    }
//                    user[0].shutdownInput();
//                    user[0].shutdownOutput();
//                    out.close();
//                    haveUser[0] = 0;
//                    user[0].close();
//                    bw.close();
//                    bufferedWriter.close();
//                    bufferedReader.close();
//                    System.out.println("shutdown函数已执行");


                } catch (IOException e) {
                    e.printStackTrace();
                }//finally {
//                    try{
//
//                        System.out.println("close函数已执行");
//
//                    }catch(IOException e){
//                        e.printStackTrace();
//                    }
//
//                }

            }
            if(jbuttonTemp == Tao){
                String s2 = "服务器消息:您已被下线，消息将不会再被递送到其他客户端@tao";
                System.out.println("获取动作成功！");
                haveUser[1] = 0;
                try {
                    for(Socket socket: Server.list) {
                        bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                        out = new PrintWriter(socket.getOutputStream());  // 对每个客户端新建相应的socket套接字
                        out.println(simpleDateFormat.format(new Date()) + s2);
                        out.flush();
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if(jbuttonTemp == Yuan){
                String s3 = "服务器消息:您已被下线，消息将不会再被递送到其他客户端@yuan";
                System.out.println("获取动作成功！");
                haveUser[2] = 0;
                try {
                    for(Socket socket: Server.list) {
                        bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                        out = new PrintWriter(socket.getOutputStream());  // 对每个客户端新建相应的socket套接字
                        out.println(simpleDateFormat.format(new Date()) + s3);
                        out.flush();
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    public void run(){
        JButton Xuan = ServerWindow.getXuan();
        JButton Tao = ServerWindow.getTao();
        JButton Yuan = ServerWindow.getYuan();
        try{
//            boolean xuan = false;
//            boolean tao = false;
//            boolean yuan = false;
            bufferedReader = new BufferedReader(new InputStreamReader(thisSocket.getInputStream()));//输入流

        while (true) {
            String str = bufferedReader.readLine();
            if (str.contains("用户[xuan]已接入")) {
                //xuan = true;
                haveUser[0] = 1;
                Xuan.setEnabled(true);
            }
            if (str.contains("用户[xuan]已从服务器断开")) {
                haveUser[0] = 0;
                Xuan.setEnabled(false);
            }
            if (str.contains("用户[tao]已接入")) {
                haveUser[1] = 1;
                Tao.setEnabled(true);
            }
            if (str.contains("用户[tao]已从服务器断开")) {
                haveUser[1] = 0;
                Tao.setEnabled(false);
            }
            if (str.contains("用户[yuan]已接入")) {
                //yuan = true;
                haveUser[2] = 1;
                Yuan.setEnabled(true);
            }
            if (str.contains("用户[yuan]已从服务器断开")) {
                haveUser[2] = 0;
                Yuan.setEnabled(false);
            }
            for(Socket socket: Server.list) {
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                out = new PrintWriter(socket.getOutputStream());  // 对每个客户端新建相应的socket套接字
                if(haveUser[0] == 1){
                    out.println(simpleDateFormat.format(new Date()) + "好友[xuan]已上线");
                    out.flush();
                }
                if(haveUser[1] == 1){
                    out.println(simpleDateFormat.format(new Date()) + "好友[tao]已上线");
                    out.flush();
                }
                if(haveUser[2] == 1){
                    out.println(simpleDateFormat.format(new Date()) + "好友[yuan]已上线");
                    out.flush();
                }
                if(haveUser[0] == 0){
                    out.println(simpleDateFormat.format(new Date()) + "好友[xuan]已下线");
                    out.flush();
                }
                if(haveUser[1] == 0){
                    out.println(simpleDateFormat.format(new Date()) + "好友[tao]已下线");
                    out.flush();
                }
                if(haveUser[2] == 0){
                    out.println(simpleDateFormat.format(new Date()) + "好友[yuan]已下线");
                    out.flush();
                }
            }

            System.out.println(str);

            // 发送给所有客户端
            for(Socket socket: Server.list) {
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                out = new PrintWriter(socket.getOutputStream());  // 对每个客户端新建相应的socket套接字
                if (socket == thisSocket) {  // 发送给当前客户端
                    out.println(simpleDateFormat.format(new Date()) + "(你)" + str);
                }
                else {  // 发送给其它客户端
                    out.println(simpleDateFormat.format(new Date()) + str);
                }
                out.flush();  // 清空out中的缓存
            }
            // 调用自定义函数输出到图形界面
            serverWindow.setTextArea(str);
        }
    } catch (Exception e) {
        Server.list.remove(thisSocket);  // 线程关闭，移除相应套接字
    }
}

}
